package com.lhb.repertory.controller;

import com.lhb.common.model.UnifyResponseObject;
import com.lhb.repertory.model.SearchFlightDTO;
import com.lhb.repertory.model.SearchFlightDTO2;
import com.lhb.repertory.model.SearchFlightVO;
import com.lhb.repertory.service.SearchService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author 86131
 * @version 1.0
 * @description
 * @date 2025/8/8 下午1:54
 */
@RestController
@RequestMapping("/repertory")
public class RepertoryController {

    @Autowired
    SearchService searchService;
    @Autowired
    RestHighLevelClient restHighLevelClient;
    @PostMapping("/search")
    public UnifyResponseObject<SearchFlightVO> searchFlight(@RequestBody SearchFlightDTO searchFlightDTO) throws IOException {
          SearchFlightVO searchFlightVO=searchService.searchFlight(searchFlightDTO);

        return UnifyResponseObject.success(searchFlightVO,"查询成功");
    }

//    @PostMapping("/searchByCondition")
//    public UnifyResponseObject searchByCondition(@RequestBody SearchByConditionDTO searchByConditionDTO)
//    {
//            searchService.searchByCondition(searchByConditionDTO);
//    }

    @PostMapping("/search2")
    public UnifyResponseObject<List<SearchFlightVO>> searchFlight2(@RequestBody SearchFlightDTO2 searchFlightDTO2) throws IOException {
        List<SearchFlightVO>searchFlightVO2=searchService.searchFlight2(searchFlightDTO2);

        return UnifyResponseObject.success(searchFlightVO2,"查询成功");
    }

    @PostMapping("/search3")
    public UnifyResponseObject<List<SearchFlightVO>> searchFlight2(@RequestBody List<SearchFlightDTO> searchFlightDTOS) throws IOException {
        List<SearchFlightVO> searchFlightVO3 =searchService.searchFlight3(searchFlightDTOS);

        return UnifyResponseObject.success(searchFlightVO3,"查询成功");
    }

    @DeleteMapping("/deleteFlight/{id}")
    public UnifyResponseObject deleteFlight(@PathVariable("id") Long id) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("repertory", id.toString());
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        // 5. 处理响应
        System.out.println("删除结果: " + response.getResult()); // 期望输出 DELETED
        System.out.println("文档ID: " + response.getId());
        System.out.println("索引名: " + response.getIndex());
        return UnifyResponseObject.success("");
    }





}
